//判断用户是否登录
let isLogin = false
$('.tocarts').hide()
//判断登录用户名
function showUserName() {
    let user = JSON.parse(localStorage.getItem('userLogin')) || []
    if (user.length == 0) {
        $('.hidden').show()
        $('.logout').hide()
        return
    } else {
        isLogin = true
        $('.username').html(user.username)
        $('.hidden').hide()
        $('.logout').show()
    }
    userLoginBycart(isLogin)
}

//退出登录
function userLogout() {
    let user = JSON.parse(localStorage.getItem('userLogin')) || []
    $('.username').html()
    $('.logout').on('click', function () {
        let flag = confirm('确定要退出登录吗？')
        if (flag == true) {
            user = []
            localStorage.setItem('userLogin', JSON.stringify(user))
            location.href = './index.html'
            $('.hidden').show()
            $('.logout').hide()
            return
        }
    })
}

//验证操作购物车
function userLoginBycart(flag) {
    if (flag == true) {
        localStorage.setItem('isLogin', JSON.stringify(flag))
        $('.tocarts').show()
        $('.loginHide').hide()
        $('.carts').on('click', function () {
            location.href = './carts.html'
            return 0
        })
    } else if (flag == false) {
        flag = localStorage.getItem('isLogin', JSON.stringify(flag))
        flag = false
        localStorage.setItem('isLogin', JSON.stringify(flag))
    }
}

showUserName()
userLogout()
userLoginBycart(isLogin)